<template>
  <div class="sc-workflow-design">
    <div class="btn-container" v-if="!disabled">
      <el-button
        type="primary"
        @click="onClickJson">
        查看 JSON
      </el-button>
    </div>
    <div class="box-scale">
      <node-wrap
        v-if="nodeConfig"
        v-model="nodeConfig" :disabled="disabled"></node-wrap>
      <div class="end-node">
        <div class="end-node-circle"></div>
        <div class="end-node-text">流程结束</div>
      </div>
    </div>
    <use-select
      v-if="selectVisible"
      :load-group-data="loadGroupDataTigger"
      :load-user-data="loadUserDataTigger"
      :load-role-data="loadRoleDataTigger"
      ref="useselect"
      @closed="selectVisible = false"></use-select>
    <el-drawer
      :visible.sync="drawer"
      size="500px"
      class="drawer"
      :append-to-body="true"
      :modal="false"
      :with-header="false">
      <div style="height: 100vh">
        <div style="padding: 1px; background-color: #3883fa">
          <el-button
            type="primary"
            plain
            @click="copyParseJson">
            复制格式化后的 JSON
          </el-button>
          <el-button
            type="primary"
            plain
            @click="copyJson">
            复制压缩后的 JSON
          </el-button>
          <el-button
            type="primary"
            plain
            @click="drawer = false">
            关闭弹窗
          </el-button>
        </div>
        <json-editor-vue
          class="editor"
          language="zh-CN"
          current-mode="view"
          v-model="value" />
      </div>
    </el-drawer>
  </div>
</template>

<script>
import nodeWrap from './nodeWrap.vue'
import useSelect from './select.vue'
import jsonEditorVue from 'vue-json-editor'
export default {
  provide() {
    return {
      select: this.selectHandle
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    value: { type: Object, default: () => {} },
    loadGroupData: {
      type: Function,
      default: null
    },
    loadUserData: {
      type: Function,
      default: null
    },
    loadRoleData: {
      type: Function,
      default: null
    }
  },
  components: {
    nodeWrap,
    useSelect,
    jsonEditorVue
  },
  data() {
    return {
      nodeConfig: this.value.nodeConfig,
      selectVisible: false,
      drawer: false,
      jsonData: null
    }
  },
  watch: {
    value(val) {
      this.nodeConfig = val.nodeConfig
    },
    nodeConfig(val) {
      this.$emit('input', val)
    }
  },
  created() {
  },
  mounted() {

  },
  methods: {
    selectHandle(type, data) {
      this.selectVisible = true
      this.$nextTick(() => {
        this.$refs.useselect.open(type, data)
      })
    },
    onClickJson () {
      this.drawer = true
    },
    loadGroupDataTigger () {
      return new Promise((reslove, reject) => {
        this.loadGroupData(reslove)
      })
    },
    loadUserDataTigger () {
      return new Promise((reslove, reject) => {
        this.loadUserData(reslove)
      })
    },
    loadRoleDataTigger () {
      return new Promise((reslove, reject) => {
        this.loadRoleData(reslove)
      })
    },
    copyParseJson () {
      this.$copyText(JSON.stringify(this.value), null, '  ').then(
        res => {
          this.$message.success('已成功复制!')
        },
        err => {
          this.$message.error('复制失败!')
        }
      )
    },
    copyJson () {
      this.$copyText(JSON.stringify(this.value)).then(
        res => {
          this.$message.success('已成功复制!')
        },
        err => {
          this.$message.error('复制失败!')
        }
      )
    }
  }
}
</script>

<style scoped lang="scss">
  @import url('index.scss');
  .editor {
    width: 500px;
    height: calc(100vh - 36px);
  }

  .editor .jsoneditor-poweredBy,
  .editor .jsoneditor-transform,
  .editor .jsoneditor-repair,
  .editor .full-screen {
    display: none !important;
  }
  .jsoneditor-menu > button.jsoneditor-copy {
    background-position: -48px 0px;
  }

  .btn-container {
    position: absolute;
    left: 16px;
    right: 0;
    display: flex;
    height: 42px;
    margin-left: 16px;
    z-index: 99;
  }

</style>
